﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using QLNhaSach;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace DataAccessLayer
{
    public class TacGiaDAL
    {
        DataService ds = new DataService();
        public TacGiaDAL()
        {
        }
        public TacGia GetTacGiaByID(int id)
        {
            TacGia tg = null;
            try
            {
                string sql = "SELECT * FROM TACGIA WHERE TG_MATACGIA=" + id;
                SqlDataReader rd = ds.ExcuteDataReader(sql);
                while (rd.Read())
                {
                    tg = new TacGia();
                    tg.MaTacGia = (int)rd["TG_MaTacGia"];
                    tg.TenTacGia = rd["TG_TenTacGia"].ToString();
                }
            }
            catch (Exception)
            {
                throw;
            }
            return tg;

        }
        public TacGia GetIDByName(string name)
        {
            TacGia tg = null;
            try
            {
                string sql = "SELECT * FROM TACGIA WHERE TG_TENTACGIA=N'" + name +"'";
                SqlDataReader rd = ds.ExcuteDataReader(sql);
                while (rd.Read())
                {
                    tg = new TacGia();
                    tg.MaTacGia = (int)rd["TG_MaTacGia"];
                    tg.TenTacGia = rd["TG_TenTacGia"].ToString();
                }
            }
            catch (Exception)
            {
                throw;
            }
            return tg;

        }
        public DataTable GetDanhSachTacGia()
        {
            DataTable tl = null;
            try
            {
                string sql = "SELECT * FROM TACGIA";
                tl = ds.GetTable(sql);
            }
            catch (Exception)
            {
                throw;
            }
            return tl;

        }
        public int InsertTacGia(TacGia tg)
        {
            int kq = 0;
            try
            {
                String sql = String.Format("INSERT INTO TACGIA(TG_TENTACGIA) VALUES (N'{0}')", tg.TenTacGia);
                kq = ds.ExcuteInsert(sql);
                ds.Close();
            }
            catch (Exception)
            {
                throw;
            }
            return kq;

        }
        public int DeleteTacGia(string maTacGia)
        {
            int kq = 0;
            try
            {
                String sql = "DELETE FROM TACGIA WHERE TG_MATACGIA=" + maTacGia;
                kq = int.Parse(ds.ExcuteInsert(sql).ToString());
            }
            catch (Exception)
            {
                throw;
            }
            return kq;

        }
        public int UpdateTacGia(TacGia tg)
        {
            int kq = 0;
            try
            {
                String sql = "UPDATE TACGIA SET TG_TENTACGIA=N'" + tg.TenTacGia + "' WHERE TG_MATACGIA=" + tg.MaTacGia;
                kq = int.Parse(ds.ExcuteInsert(sql).ToString());
            }
            catch (Exception)
            {
                throw;
            }
            return kq;

        }
    }
}
